package com.huobao.yunying.activitys;

import java.util.List;
import java.util.Map;

import org.xutils.view.annotation.ViewInject;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huobao.yunying.adapters.Adapter_News;
import com.huobao.yunying.base.HuobaoBaseActivity;
import com.huobao.yunying.entity.News;
import com.huobao.yunying.https.Http;
import com.huobao.yunying.https.HttpListener;
import com.huobao.yunying.https.HttpUtil;
import com.huobao.yunying.utils.ImageUtils;
import com.huobao.yunying.views.NoScrollListView;
import com.huobao.zhangying.R;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;

public class Act_Dongtai_List extends HuobaoBaseActivity implements OnItemClickListener{

	@ViewInject(R.id.img_1)
	private ImageView img1;
	@ViewInject(R.id.img_2)
	private ImageView img2;
	@ViewInject(R.id.listview)
	private NoScrollListView listview;
	
	private Adapter_News adapter;
	@Override
	protected int getContentView() {
		// TODO Auto-generated method stub
		return R.layout.layout_dongtai_list;
	}

	@Override
	protected String getTitleText() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected void init() {
		adapter = new Adapter_News(context);
		listview.setAdapter(adapter);
		listview.setOnItemClickListener(this);
		mAbTitleBar.setVisibility(View.GONE);
		getData();
		getBewsList();
	}
	
	private void getData(){
		Map<String, String> map = HttpUtil.getParams("app","news");
		Http.getInstant().sendRequest(context, map, new HttpListener() {
			
			@Override
			public void onHttpSuccess(String result) {
				// TODO Auto-generated method stub
				JSONObject jsonObject = JSON.parseObject(result);
				jsonObject = jsonObject.getJSONObject("retval");
				String logo = jsonObject.getString("logo");
				String logo1 = jsonObject.getString("logo_1");
				ImageUtils.loadimg(img1, logo);
				ImageUtils.loadimg(img2, logo1);
			}
			
			@Override
			public void onHttpError(String error) {
				// TODO Auto-generated method stub
				
			}
		});
	}
	private void getBewsList(){
		Map<String, String> map = HttpUtil.getParams("app","news","act","newslist");
		Http.getInstant().sendRequest(context, map, new HttpListener() {
			
			@Override
			public void onHttpSuccess(String result) {
				JSONObject jsonObject = JSON.parseObject(result);
				List<News> list = JSON.parseArray(jsonObject.getString("retval"), News.class);
				adapter.addAll(list);
			}
			
			@Override
			public void onHttpError(String error) {
				// TODO Auto-generated method stub
				
			}
		});
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		Intent intent = new Intent(this, Act_Dongtai_Detail.class);
		intent.putExtra("id", adapter.getItem(position).article_id);
		startActivity(intent);
	}

}
